package com.link2room.rodimus.facade.impl;


import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.constant.MallGoodsConstant;
import com.link2room.rodimus.dto.param.base.MallGoodPriceTypeDto;
import com.link2room.rodimus.dto.param.base.MallGoodTypeDto;
import com.link2room.rodimus.dto.param.manager.*;
import com.link2room.rodimus.dto.result.base.*;
import com.link2room.rodimus.entity.*;

import com.link2room.rodimus.facade.IMallManagerFacadeService;
import com.link2room.rodimus.service.IMallService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.*;

/**
 * Created by VegetaKo on 2017/1/20.
 */
@Service("mallManagerFacadeService")
public class MallManagerFacadeServiceImpl implements IMallManagerFacadeService {

    @Resource
    private ICommonService commonService;
    @Resource
    private IMallService mallService;

    @Override
    public QueryResult<MallOrderBaseDto> getOrders(MallOrderManagerParamDto mallOrderManagerParamDto) throws Exception {
        String lhotelGroupCode = mallOrderManagerParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null.");
        HashMap<String, String> orderMap = new HashMap<String, String>();
        orderMap.put(CouponConsumeBaseEntity.BaseEntity_.id.toString(), CriteriaUtil.DESC.toString());


        MallOrderBase mallOrderBase = new MallOrderBase();
        mallOrderBase.setSta(mallOrderManagerParamDto.getStas());
        mallOrderBase.setLhotelGroupCode(lhotelGroupCode);

        QueryResult<MallOrderBase> mallOrderBaseQueryResult = commonService.findAllEQ(MallOrderBase.class, mallOrderBase, mallOrderManagerParamDto.getCurrentPage(), mallOrderManagerParamDto.getPageSize(), orderMap, null);
        QueryResult<MallOrderBaseDto> res = ClassConverUtil.copyQueryResultToDtoQueryResult(mallOrderBaseQueryResult, MallOrderBaseDto.class, false);
        return res;
    }

    @Override
    public MallOrderBaseDto getOrder(MallOrderManagerParamDto mallOrderManagerParamDto) throws Exception {
        String lhotelGroupCode = mallOrderManagerParamDto.getLhotelGroupCode();
        MallOrderBase mallOrderBase = new MallOrderBase();
        mallOrderBase.setId(mallOrderManagerParamDto.getId());
        mallOrderBase = commonService.findOneEQ(MallOrderBase.class, mallOrderBase);
        MallOrderBaseDto mallOrderBaseDto = ClassConverUtil.copyPropertiesToDto(mallOrderBase, MallOrderBaseDto.class, false);
        //MallOrderPriceDto mallOrderPriceDto = new MallOrderPriceDto();
        //mallOrderPriceDto.setLhotelGroupCode(mallOrderManagerParamDto.getLhotelGroupCode() );
        MallOrderPrice mallOrderPrice = new MallOrderPrice();
        mallOrderPrice.setLhotelGroupCode(lhotelGroupCode);
        mallOrderPrice.setOrderCode(mallOrderBase.getOrderCode());
        QueryResult<MallOrderPrice> mallOrderPriceQueryResult = commonService.findAllEQ(MallOrderPrice.class, mallOrderPrice);
        //.setMallOrderPriceDtos(ClassConverUtil.copyPropertiesListToDtoList(mallOrderPriceQueryResult.getResultlist(), MallOrderPriceDto.class,true));
        QueryResult<MallOrderPriceDto> mallOrderPriceDtoQueryResult = ClassConverUtil.copyQueryResultToDtoQueryResult(mallOrderPriceQueryResult, MallOrderPriceDto.class, true);
        for (MallOrderPriceDto mopd : mallOrderPriceDtoQueryResult.getResultlist()) {
            MallGoodBase mallGoodBase = new MallGoodBase();
            mallGoodBase.setGoodCode(mopd.getGoodCode());
            mallGoodBase = commonService.findOneEQ(MallGoodBase.class, mallGoodBase);
            mopd.setName(mallGoodBase.getName());
            mopd.setLogo(mallGoodBase.getLogo());
            mopd.setPictures(mallGoodBase.getPictures());
            // mallOrderBaseDto.setLogo(mallGoodBase.getLogo()  );
        }
        mallOrderBaseDto.setMallOrderPriceDtos(mallOrderPriceDtoQueryResult.getResultlist());

        return mallOrderBaseDto;
    }

    @Override
    public void modifyOrder(MallOrderManagerParamDto mallOrderManagerParamDto) throws Exception {
        MallOrderBase mallOrderBase = new MallOrderBase();
        mallOrderBase.setId(mallOrderManagerParamDto.getId());
        mallOrderBase = commonService.findOneEQ(MallOrderBase.class, mallOrderBase);

        mallOrderBase.setSta(mallOrderManagerParamDto.getMallOrderBaseDto().getSta());
        //mallOrderBase.setExpressAddress( (mallOrderManagerParamDto.getMallOrderBaseDto().getExpressAddress());
        //mallOrderBase.setExpressMan(mallOrderManagerParamDto.getMallOrderBaseDto().getExpressMan());
        //mallOrderBase.setExpressMobile(mallOrderManagerParamDto.getMallOrderBaseDto().getExpressMobile());
        mallOrderBase.setExpressNo(mallOrderManagerParamDto.getMallOrderBaseDto().getExpressNo());
        mallOrderBase.setExpressProvider(mallOrderManagerParamDto.getMallOrderBaseDto().getExpressProvider());
        mallOrderBase.setExpressSta(mallOrderManagerParamDto.getMallOrderBaseDto().getExpressSta());
        commonService.merge(mallOrderBase);
    }

    @Override
    public QueryResult<MallGoodBaseApiDto> getGoods(MallGoodsManagerParamDto mallGoodsManagerParamDto) throws Exception {
        String lhotelGroupCode = mallGoodsManagerParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null.");
        MallGoodBase mallGoodBase = new MallGoodBase();
        mallGoodBase.setGoodCategory(mallGoodsManagerParamDto.getGoodCategory());
        mallGoodBase.setIsHalt(mallGoodsManagerParamDto.getIsHalt());
        mallGoodBase.setLhotelGroupCode(lhotelGroupCode);
        QueryResult<MallGoodBase> mallGoodBaseQueryResult = commonService.findAllEQ(MallGoodBase.class, mallGoodBase, mallGoodsManagerParamDto.getCurrentPage(), mallGoodsManagerParamDto.getPageSize());
        QueryResult<MallGoodBaseApiDto> res = ClassConverUtil.copyQueryResultToDtoQueryResult(mallGoodBaseQueryResult, MallGoodBaseApiDto.class, true);
        if (res != null && CollectionUtils.isNotEmpty(res.getResultlist())){
            res.getResultlist().stream().forEach(mallGoodBaseDto->{
                String goodType = mallGoodBaseDto.getGoodType();
                String extra = mallGoodBaseDto.getGoodExtra();
                if(StringUtil.isBlank(extra)){return;}
                if(MallGoodsConstant.GoodType.PMSCOUPON.equals(goodType)){
                    MallGoodBaseExtraPMSCouponDto couponDto = JacksonUtil.jsonToBean(extra, MallGoodBaseExtraPMSCouponDto.class);
                    mallGoodBaseDto.setMallGoodBaseExtraPMSCoupon(couponDto);
                }else if (MallGoodsConstant.GoodType.PMSPACKAGE.equals(goodType)) {
                    mallGoodBaseDto.setMallGoodBaseExtraPMSPackage(JacksonUtil.jsonToBean(extra, MallGoodBaseExtraPMSPackageDto.class));
                }
            });
        }
        return res;
    }

    @Override
    public void modifyGood(MallGoodsManagerParamDto mallGoodsManagerParamDto) throws Exception {
        MallGoodBase mallGoodBase;
        if (null != mallGoodsManagerParamDto.getMallGoodBaseDto().getId()) {
            mallGoodBase = commonService.findOneById(MallGoodBase.class, mallGoodsManagerParamDto.getMallGoodBaseDto().getId());
            if (mallGoodBase != null) {
                ClassConverUtil.copyProperties(mallGoodsManagerParamDto.getMallGoodBaseDto(), mallGoodBase);
                commonService.merge(mallGoodBase);
            }
        }
    }

    @Override
    public void updateMallGood(MallGoodsUpdateParamDto param) throws Exception {
        MallGoodBaseApiDto mallGoodBaseDto = param.getMallGoodBaseDto();
        L2RAssert.isTrue(mallGoodBaseDto == null, new SysException(MALL_GOOD_UPDATE_EROOR, "[mallGoodBaseDto] is required."));
        String goodCategoryCode = mallGoodBaseDto.getGoodCategory();
        L2RAssert.stringBlank(goodCategoryCode, "[goodCategoryCode] is required");
        String lhotelGroupCode = param.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required");
        MallGoodCategory goodCategory = findCategroy(lhotelGroupCode, goodCategoryCode);
        L2RAssert.isTrue(goodCategory == null, new SysException(MALL_GOOD_UPDATE_EROOR, "产品类目不存在"));
        String goodCode = mallGoodBaseDto.getGoodCode();
        L2RAssert.stringBlank(goodCode, "[goodCode] is required.");
        MallGoodBase findGoodBase = new MallGoodBase();
        findGoodBase.setGoodCode(goodCode);
        findGoodBase.setLhotelGroupCode(lhotelGroupCode);
        MallGoodBase goodBase = commonService.findOneEQ(MallGoodBase.class, findGoodBase);
        Long id = goodBase.getId();
        String extra = "";
        if(MallGoodsConstant.GoodType.PMSCOUPON.equals(mallGoodBaseDto.getGoodType())){
           extra = JacksonUtil.beanToJson(mallGoodBaseDto.getMallGoodBaseExtraPMSCoupon());
        }else if(MallGoodsConstant.GoodType.PMSPACKAGE.equals(mallGoodBaseDto.getGoodType())){
            extra = JacksonUtil.beanToJson(mallGoodBaseDto.getMallGoodBaseExtraPMSPackage());
        }
        ClassConverUtil.copyProperties(mallGoodBaseDto, goodBase);
        goodBase.setId(id);
        goodBase.setGoodExtra(extra);
        commonService.merge(goodBase);
    }

    @Override
    public MallGoodBaseApiDto getGoodByCode(MallGoodsFindParamDto paramDto) throws Exception {
        String goodCode = paramDto.getGoodCode();
        L2RAssert.stringBlank(goodCode, "[goodCode] is null.");
        MallGoodBase find = new MallGoodBase();
        find.setGoodCode(goodCode);
        find.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        MallGoodBase qres = commonService.findOneEQ(MallGoodBase.class, find);
        if (qres == null) {
            return null;
        }
        MallGoodBaseApiDto mallGoodBaseDto = ClassConverUtil.copyPropertiesToDto(qres, MallGoodBaseApiDto.class, true);
        MallGoodPrice mallGoodPriceFinder = new MallGoodPrice();
        mallGoodPriceFinder.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        mallGoodPriceFinder.setGoodCode(mallGoodBaseDto.getGoodCode());
        QueryResult<MallGoodPrice> mallGoodPriceDtoQuery = commonService.findAllEQ(MallGoodPrice.class, mallGoodPriceFinder);
        QueryResult<MallGoodPriceDto> mallGoodPriceDtoQueryResult = ClassConverUtil.copyQueryResultToDtoQueryResult(mallGoodPriceDtoQuery, MallGoodPriceDto.class, true);
        mallGoodBaseDto.setPriceDtoList(mallGoodPriceDtoQueryResult.getResultlist());
        String goodType = mallGoodBaseDto.getGoodType();
        String extra = mallGoodBaseDto.getGoodExtra();
        if(StringUtil.isNotBlank(extra)&& MallGoodsConstant.GoodType.PMSCOUPON.equals(goodType)){
            MallGoodBaseExtraPMSCouponDto couponDto = JacksonUtil.jsonToBean(extra, MallGoodBaseExtraPMSCouponDto.class);
            mallGoodBaseDto.setMallGoodBaseExtraPMSCoupon(couponDto);
        }else if (StringUtil.isNotBlank(extra)&&MallGoodsConstant.GoodType.PMSPACKAGE.equals(goodType)) {
            mallGoodBaseDto.setMallGoodBaseExtraPMSPackage(JacksonUtil.jsonToBean(extra, MallGoodBaseExtraPMSPackageDto.class));
        }
        return mallGoodBaseDto;
    }

    @Override
    public void createMallGood(MallGoodCreateParamDto paramDto) throws Exception {
        String goodCode = paramDto.getGoodCode();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        MallGoodBase mallGood = _getMallGood(lhotelGroupCode, goodCode);
        L2RAssert.isTrue(mallGood != null, new SysException(MALL_GOOD_CREATE_EROOR_duplicate));
        String name = paramDto.getName();
        L2RAssert.stringBlank(name, "[name] is required.");
        MallGoodBase find = new MallGoodBase();
        find.setGoodCode(goodCode);
        find.setName(name);
        long count = commonService.getCountAllEQ(MallGoodBase.class, find, null);
        L2RAssert.isTrue(count > 0, new SysException(MALL_GOOD_CREATE_EROOR_name_duplicate, "商品名称重复"));
        String goodCategory = paramDto.getGoodCategory();
        L2RAssert.stringBlank(goodCategory, "[goodCategory] is required.");
        MallGoodCategory goodCategoryCount = findCategroy(lhotelGroupCode, goodCategory);
        L2RAssert.isTrue(null == goodCategoryCount, new SysException(MALL_GOOD_CREATE_EROOR_Category_NOT_EXIT, "商品类目不存在"));
        MallGoodBase goodBase = ClassConverUtil.copyProperties(paramDto, MallGoodBase.class);
        if(MallGoodsConstant.GoodType.PMSCOUPON.equals(paramDto.getGoodType())){
            String extra = JacksonUtil.beanToJson(paramDto.getMallGoodBaseExtraPMSCoupon());
            goodBase.setGoodExtra(extra);
        }else if(MallGoodsConstant.GoodType.PMSPACKAGE.equals(paramDto.getGoodType())){
            String extra = JacksonUtil.beanToJson(paramDto.getMallGoodBaseExtraPMSPackage());
            goodBase.setGoodExtra(extra);
        }
        commonService.save(goodBase);
    }

    private MallGoodBase _getMallGood(String lhotelGroupCode, String goodCode) throws Exception {
        MallGoodBase find = new MallGoodBase();
        find.setGoodCode(goodCode);
        find.setLhotelGroupCode(lhotelGroupCode);
        return commonService.findOneEQ(MallGoodBase.class, find);
    }

    @Override
    public QueryResult<MallGoodCategoryDto> getGoodCategorise(MallGoodCategoryFindParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required");

        String isHalt = paramDto.getIsHalt();
        if (StringUtil.isNotBlank(isHalt)) {
            L2RAssert.ishaltVaild(isHalt);
        }
        MallGoodCategory find = new MallGoodCategory();
        find.setIsHalt(isHalt);
        find.setLhotelGroupCode(lhotelGroupCode);
        QueryResult<MallGoodCategory> qres = commonService.findAllEQ(MallGoodCategory.class, find, paramDto.getCurrentPage(), paramDto.getCurrentPage());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres, MallGoodCategoryDto.class, true);
    }

    @Override
    public void createGoodCategories(MallGoodCategoryCreateParamDto paramDto) throws Exception {
        String categroyCode = paramDto.getGoodCategoryCode();
        L2RAssert.stringBlank(categroyCode, "[categroyCode] is required");
        String name = paramDto.getName();
        L2RAssert.stringBlank(name, "[name] is required");
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required");
        String logo = paramDto.getLogo();
        L2RAssert.isTrue(findCategroy(lhotelGroupCode, categroyCode) != null, new SysException(MALL_GOOD_CATEGORY_CREATE_EROOR, "类目代码已存在"));
        L2RAssert.isTrue(countName(lhotelGroupCode, name) > 0, new SysException(MALL_GOOD_CATEGORY_CREATE_EROOR, "类目名称已存在"));
        MallGoodCategory newEntity = new MallGoodCategory();
        newEntity.setLhotelGroupCode(lhotelGroupCode);
        newEntity.setGoodCategroyCode(categroyCode);
        newEntity.setLogo(logo);
        newEntity.setName(name);
        commonService.save(newEntity);
    }

    @Override
    public MallGoodCategoryDto getMallGoodCategory(MallGoodCategoryFindParamDto paramDto) throws Exception {
        String categoryCode = paramDto.getGoodCategoryCode();
        L2RAssert.stringBlank(categoryCode, "[categoryCode] is required");
        MallGoodCategory qres = this._findMallGoodCategory(paramDto.getLhotelGroupCode(), categoryCode);
        if (qres == null) {
            return null;
        } else {
            return ClassConverUtil.copyProperties(qres, MallGoodCategoryDto.class);
        }
    }

    @Override
    public void updateMallGoodCategory(MallGoodCategoryUpdateParamDto paramDto) throws Exception {
        String categroyCode = paramDto.getGoodCategoryCode();
        L2RAssert.stringBlank(categroyCode, "[categroyCode] is required");
        MallGoodCategory mallGoodCategoryEntity = this._findMallGoodCategory(paramDto.getLhotelGroupCode(), categroyCode);
        L2RAssert.isTrue(mallGoodCategoryEntity == null, new SysException(MALL_GOOD_CATEGORY_UPDATE_EROOR, "该类目不存在"));
        String isHalt = paramDto.getIsHalt();
        L2RAssert.ishaltVaild(isHalt);
        String name = paramDto.getName();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();

        L2RAssert.isTrue(countName(lhotelGroupCode, name) > 0, new SysException(MALL_GOOD_CATEGORY_UPDATE_EROOR, "类目名称已存在"));
        mallGoodCategoryEntity.setName(name);
        mallGoodCategoryEntity.setIsHalt(isHalt);
        commonService.merge(mallGoodCategoryEntity);
    }

    @Override
    public void deleteMallGoodCategory(MallGoodCategoryDeleteParamDto paramDto) throws Exception {
        String goodCategoryCode = paramDto.getGoodCategoryCode();
        L2RAssert.stringBlank(goodCategoryCode, "[goodCategoryCode] is required");
        MallGoodCategory mallGoodCategoryEntity = this._findMallGoodCategory(paramDto.getLhotelGroupCode(), goodCategoryCode);
        if (mallGoodCategoryEntity != null) {
            commonService.deleteAllEQ(MallGoodCategory.class, mallGoodCategoryEntity);
        }
    }

    @Override
    public QueryResult<MallGoodTypeDto> getMallGoodType(BaseL2rParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String isHalt = paramDto.getIsHalt();
        MallGoodTypeEntity find = new MallGoodTypeEntity();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setIsHalt(isHalt);
        QueryResult<MallGoodTypeEntity> qres = commonService.findAllEQ(MallGoodTypeEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres, MallGoodTypeDto.class, true);
    }

    @Override
    public QueryResult<MallGoodPriceDto> mallGoodPrices(MallGoodPriceFindParamDto priceFindParamDto) throws Exception {
        String goodCode = priceFindParamDto.getGoodCode();
        String priceCode = priceFindParamDto.getPriceCode();
        String priceType = priceFindParamDto.getPriceType();
        String tag = priceFindParamDto.getTag();
        MallGoodPrice find = new MallGoodPrice();
        find.setGoodCode(goodCode);
        find.setPriceCode(priceCode);
        find.setPriceType(priceType);
        find.setTag(tag);
        find.setLhotelGroupCode(priceFindParamDto.getLhotelGroupCode());
        QueryResult<MallGoodPrice> qres = commonService.findAllEQ(MallGoodPrice.class, find, priceFindParamDto.getCurrentPage(), priceFindParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres, MallGoodPriceDto.class, true);
    }

    @Override
    public void createMallGoodPrices(MallGoodPriceCreateParamDto paramDto) throws Exception {
        List<MallGoodPriceDto> goodPrices = paramDto.getMallGoodPrices();
        L2RAssert.isTrue(CollectionUtils.isEmpty(goodPrices), new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[goodPrices] is required."));
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        List<String> priceTypes = mallGoodPriceTpyes(paramDto).getResultlist()
                .stream()
                .map(MallGoodPriceTypeDto::getPriceType)
                .collect(Collectors.toList());
        List<MallGoodPrice> goodPriceEntities = goodPrices.stream().filter(goodPrice -> {
            String priceCode = goodPrice.getPriceCode();
            L2RAssert.stringBlank(priceCode, "[priceCode] is required.");
            L2RAssert.isTrue(priceCodeCount(lhotelGroupCode, priceCode) > 0, new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[priceCode] " + priceCode + " 重复 "));
            String priceType = goodPrice.getPriceType();
            L2RAssert.isTrue(!priceTypes.contains(priceType), new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[priceType]不存在."));
            BigDecimal money = goodPrice.getMoney();
            L2RAssert.isTrue(money == null || BigDecimal.ZERO.compareTo(money) > 0, new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[money] 不合法."));
            BigDecimal point = goodPrice.getPoint();
            L2RAssert.isTrue(point == null || BigDecimal.ZERO.compareTo(point) > 0, new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[point] 不合法."));
            String goodCode = goodPrice.getGoodCode();
            L2RAssert.stringBlank(goodCode, "[goodCode] is required.");
            L2RAssert.isTrue(goodCodeCount(lhotelGroupCode, goodCode) <= 0, new SysException(MALL_GOOD_PRICE_CREATE_EROOR, "[goodCode] " + goodCode + " 商品不存在."));
            return true;
        }).map(this::generateMallGoodPrice)
                .collect(Collectors.toList());
        //是否需要删除其他
        Object[] toDeleteIds = null;
        if(DefaultValue.T.equals(paramDto.getDeleteOther())){
            toDeleteIds = goodPriceEntities.stream()
                    .flatMap(entitis -> {
                        String goodCode = entitis.getGoodCode();
                        MallGoodPrice find = new MallGoodPrice();
                        find.setGoodCode(goodCode);
                        find.setLhotelGroupCode(lhotelGroupCode);
                        try {
                            QueryResult<MallGoodPrice> qres = commonService.findAllEQ(MallGoodPrice.class, find);
                            if (CollectionUtils.isNotEmpty(qres.getResultlist())) {
                                return qres.getResultlist().stream();
                            } else {
                                return Stream.empty();
                            }
                        } catch (Exception e) {
                            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
                        }
                    })
                    .map(BaseEntity::getId)
                    .toArray();
        }
        mallService.saveMallGoodPrices(goodPriceEntities,toDeleteIds);
    }

    @Override
    public void updateMallGoodPrices(MallGoodPriceUpdateParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required.");
        MallGoodPriceDto newGoodGoodPrice = paramDto.getMallGoodPrice();
        L2RAssert.isTrue(null == newGoodGoodPrice, new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[goodGoodPrice] is required."));
        String priceCode = newGoodGoodPrice.getPriceCode();
        L2RAssert.stringBlank(priceCode, "[priceCode] is required.");
        List<String> priceTypes = mallGoodPriceTpyes(paramDto).getResultlist()
                .stream()
                .map(MallGoodPriceTypeDto::getPriceType)
                .collect(Collectors.toList());
        MallGoodPrice find = new MallGoodPrice();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setPriceCode(priceCode);
        MallGoodPrice goodPrice = commonService.findOneEQ(MallGoodPrice.class, find);
        L2RAssert.isTrue(null == goodPrice, new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[priceCode]" + priceCode + " 不存在."));
        String priceType = goodPrice.getPriceType();
        L2RAssert.isTrue(!priceTypes.contains(priceType), new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[priceType]不存在."));
        BigDecimal money = goodPrice.getMoney();
        L2RAssert.isTrue(money == null || BigDecimal.ZERO.compareTo(money) > 0, new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[money] 不合法."));
        BigDecimal point = goodPrice.getPoint();
        L2RAssert.isTrue(point == null || BigDecimal.ZERO.compareTo(point) > 0, new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[point] 不合法."));
        String goodCode = goodPrice.getGoodCode();
        L2RAssert.stringBlank(goodCode, "[goodCode] is required.");
        L2RAssert.isTrue(goodCodeCount(lhotelGroupCode, goodCode) <= 0, new SysException(MALL_GOOD_PRICE_UPDATE_EROOR, "[goodCode] " + goodCode + " 商品不存在."));
        Long id = goodPrice.getId();
        ClassConverUtil.copyProperties(newGoodGoodPrice, goodPrice);
        goodPrice.setId(id);
        commonService.merge(goodPrice);
    }

    @Override
    public void deleteMallGoodPrice(MallGoodPriceDeleteParamDto paramDto) throws Exception {
        String priceCode = paramDto.getPriceCode();
        L2RAssert.stringBlank(priceCode, "[priceCode] is required.");
        MallGoodPrice find = new MallGoodPrice();
        find.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        find.setPriceCode(priceCode);
        MallGoodPrice deleteEntity = commonService.findOneEQ(MallGoodPrice.class, find);
        if (null == deleteEntity) {
            return;
        } else {
            commonService.deleteAllEQ(MallGoodPrice.class, deleteEntity);
        }
    }

    private MallGoodPrice generateMallGoodPrice(MallGoodPriceDto mallGoodPriceDto) {
        return ClassConverUtil.copyProperties(mallGoodPriceDto, MallGoodPrice.class);
    }

    private long goodCodeCount(String lhotelGroupCode, String goodCode) {
        MallGoodBase find = new MallGoodBase();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setGoodCode(goodCode);
        try {
            return commonService.getCountAllEQ(MallGoodBase.class, find, null);
        } catch (Exception e) {
            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
        }
    }

    private long priceCodeCount(String lhotelGroupCode, String priceCode) {
        L2RAssert.stringBlank(priceCode, "[priceCode] is required.");
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required.");
        MallGoodPrice find = new MallGoodPrice();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setPriceCode(priceCode);
        try {
            return commonService.getCountAllEQ(MallGoodPrice.class, find, null);
        } catch (Exception e) {
            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
        }
    }


    @Override
    public QueryResult<MallGoodPriceTypeDto> mallGoodPriceTpyes(BaseL2rParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String isHalt = paramDto.getIsHalt();
        MallGoodPriceTypeEntity find = new MallGoodPriceTypeEntity();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setIsHalt(isHalt);
        QueryResult<MallGoodPriceTypeEntity> qres = commonService.findAllEQ(MallGoodPriceTypeEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres, MallGoodPriceTypeDto.class, true);
    }


    private long countName(String lhotelGroupCode, String name) throws Exception {
        MallGoodCategory countName = new MallGoodCategory();
        countName.setLhotelGroupCode(lhotelGroupCode);
        countName.setName(name);
        return commonService.getCountAllEQ(MallGoodCategory.class, countName, null);
    }

    private MallGoodCategory _findMallGoodCategory(String lhotelGroupCode, String categoryCode) throws Exception {
        MallGoodCategory find = new MallGoodCategory();
        find.setGoodCategroyCode(categoryCode);
        find.setLhotelGroupCode(lhotelGroupCode);
        return commonService.findOneEQ(MallGoodCategory.class, find);
    }

    private MallGoodCategory findCategroy(String lhotelGroupCode, String goodCategoryCode) throws Exception {
        MallGoodCategory countCondition = new MallGoodCategory();
        countCondition.setLhotelGroupCode(lhotelGroupCode);
        countCondition.setGoodCategroyCode(goodCategoryCode);
        return commonService.findOneEQ(MallGoodCategory.class, countCondition);
    }
}
